﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class AB05AV95N : DocumentBase
    {
        #region-------------Properties-----------------------
        int ThreadCount = 0;
        int finishedcount = 0;
        int totalrecord = 0;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet setUI= new DataSet();
        public DataSet setUI1 = new DataSet();
        //public INTEXDataSet setWorker=new INTEXDataSet();
        AB05AV95NData m_Data, m_DataX;
        public DataSet setResult;
        DataTable tableUI;
        
        private DataRow dwUIABPOS;
        
        public AB05AV95N()
        {
            InitializeComponent();
            this.Name = "后整理生产状态报表";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
           

            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            //setUI = (INTEXDataSet)TryFindResource("iNTEXDataSet");
            m_Data = new AB05AV95NData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("ABPERLKZ", typeof(string));
            tableUI.Columns.Add("ABPAUFKRS", typeof(string));
            tableUI.Columns.Add("ABPAUFLFD", typeof(string));
            tableUI.Columns.Add("ABPAUFPOS", typeof(string));
            tableUI.Columns.Add("ARTICLE", typeof(string));
            tableUI.Columns.Add("STARTDATE", typeof(string));
            tableUI.Columns.Add("ENDDATE", typeof(string));
            tableUI.Columns.Add("TP", typeof(string));
        }
        #endregion
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                finishedcount = 0;
                setUI.Clear();
                zoneQueryCond.IsExpanded = false;
                setUI.AcceptChanges();
                dwUIABPOS.Delete();
                dwUIABPOS["ABPERLKZ"] = " ";
                dwUIABPOS["TP"] = " ";
                string KRS = txtKRS.Text.Trim();
                string LFD = txtLFD.Text.Trim();
                string POS = txtPOS.Text.Trim();

                string ERLKZ = txtERLKZ.IsChecked.Value.ToString();
                if (ERLKZ == "True")
                {
                    dwUIABPOS["ABPERLKZ"] = " ";
                }
                else
                {
                    dwUIABPOS["ABPERLKZ"] = "J";
                }
                string TP = txtTP.IsChecked.Value.ToString();
                if (TP == "True")
                {
                    dwUIABPOS["TP"] = " ";
                }
                else
                {
                    dwUIABPOS["TP"] = "J";
                }
                dwUIABPOS["ABPAUFKRS"] = KRS;
                dwUIABPOS["ABPAUFLFD"] = "";
                if (LFD.Trim().Length > 0)
                {
                    dwUIABPOS["ABPAUFLFD"] = m_Data.Right("000000" + LFD, 6);
                }
                dwUIABPOS["ABPAUFPOS"] = "";
                if (POS.Trim().Length > 0)
                {
                    dwUIABPOS["ABPAUFPOS"] = m_Data.Right("000" + POS, 3);
                }

                dwUIABPOS["ARTICLE"] = txtArticle.Text.ToString().Trim();
                string GISSUEStartDate = gsendstartdate.Text.ToString().Trim();
                string GISSUEEndDate = gsendenddate.Text.ToString().Trim();
                if (GISSUEStartDate == "")
                {
                    GISSUEStartDate = "0";
                }
                if (GISSUEEndDate == "")
                {
                    GISSUEEndDate = "29990101";
                }
                dwUIABPOS["STARTDATE"] = GISSUEStartDate;
                dwUIABPOS["ENDDATE"] = GISSUEEndDate;
                
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    setResult = m_Data.GetABPOS(dwUIABPOS);
                    setUI.Merge(setResult);
                    ABPOSDataGrid.DataContext = setUI;
                     
                }
                
            }
            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);

            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                txtKRS.Text = "";
                dwUIABPOS = tableUI.NewRow();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        private void gsendstartDateChooser_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            gsendstartdate.Text = m_Data.GetDate(sender);
        }
        private void gsendendDateChooser_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            gsendenddate.Text = m_Data.GetDate(sender);
        }
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                m_DataX = new AB05AV95NData(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                AB05AV95NReport m_rpt = new AB05AV95NReport(dicParamater);
                m_rpt.dsSource = setUI;
                m_rpt.PrintType = "JustExcel";
                m_rpt.exAgent = m_DataX.exAgent;
                //m_rpt.StandardType = "AB05AV95N";
                m_rpt.WriteExcelReport("EXCEL"); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void ItemsControls_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {

                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;  
                     
                }
             
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
